/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package practiceexamples;

import java.awt.event.ActionEvent;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;

/**
 *
 * @author rdisalvo
 */
public class FreezeFrame extends JFrame {

    public FreezeFrame(String s) {
        super(s);
    }

    public static void main(String[] args) {
        JButton freeze = new JButton(new FreezeGUIButtonAction());
        JButton noFreeze = new JButton(new DontFreezeGUIButtonAction());

        FreezeFrame frame = new FreezeFrame("Freeze the GUI!");
        frame.setSize(400,300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        centerJFrame(frame);
        frame.setLayout(new java.awt.FlowLayout());
        frame.add(freeze);
        frame.add(noFreeze);
        frame.setVisible(true);
    }

    /**
     * Just a method to center the frame to the screen.
     * @param frame
     */

    public static void centerJFrame(JFrame frame) {
        java.awt.Dimension d = frame.getToolkit().getScreenSize();
        java.awt.Dimension d2 = frame.getSize();
        int x = d.width / 2 - d2.width / 2;
        int y = d.height / 2 - d2.height / 2;
        frame.setLocation(x, y);
    }

    /**
     * The button that is built atop this action will loop forever--INSIDE THE ACTIONPERFORMED METHOD
     * which means that it will be looping forever, INSIDE THE EVENTQUEUE
     * The SWING GUI will FREEZE UP therefore!
     * Obviously this is an extreme case, but if you have a lot of stuff going on a realistic
     * case will approach this type of unresponsiveness.
     */
    static class FreezeGUIButtonAction extends AbstractAction{
        public FreezeGUIButtonAction(){
            this.putValue(Action.NAME, "AN INFINITE LOOP THAT FREEZES YOUR GUI!");
        }
        public void actionPerformed(ActionEvent e) {
           while(true){
                try {
                    Thread.sleep(100);
                } catch (InterruptedException ex) {
                    Logger.getLogger(FreezeFrame.class.getName()).log(Level.SEVERE, null, ex);
                }
               System.out.println(" F ");
           }
        }

    }

    /**
     * In this, you click on the button, and it spins off a new thread, so that
     * the GUI is not locked up. This is how our code should be implemented, of course!
     */
    static class DontFreezeGUIButtonAction extends AbstractAction{
        public DontFreezeGUIButtonAction(){
            this.putValue(Action.NAME,"AN INFINITE LOOP--THAT DOESN'T FREEZE THE GUI!");
        }
        public void actionPerformed(ActionEvent e){
            Thread th = new Thread(new InfiniteLoopRun());
            th.start();
        }
    }
    static class InfiniteLoopRun implements Runnable{

        public void run(){
            while(true){
                try {
                    Thread.sleep(100);
                } catch (InterruptedException ex) {
                }
                System.out.println(" OK! ");
            }
        }
    }
}
